<?php
/**
 * Klasse zur Erzeugung eines Bootstrap Carousel
 *
 * @author		Joachim Ruf, Loresoft Software (http://www.loresoft.de)
 * @license		New BSD License
 * @version		Class for Bootstrap v2.3.2
 *
 * */


Class Bootstrap_Nav_Handler
{
	private $arrList			= array();
	private $type				= 'nav-tabs'; // {nav-tab|nav-pill|nav-list|nav-stacked}
	private $align				= ''; // { |tabs-below|tabs-left|tabs-right}
	private $active				= NULL;
	private $baseurl			= '';


	/**
	 *
	 * */
	function __construct ($_baseUrl = NULL) {
		$this->baseurl	= $_baseUrl;
	}


	/**
	 * Definiert den Nav-Typ
	 * @param string $_type		: {nav-tab|nav-pill|nav-list|nav-stacked}
	 * */
	public function setType ($_type) {
		$this->type = $_type;
	}


	/**
	 * Definiert die Anzeigeposition
	 * @param string $_type		: { |tabs-below|tabs-left|tabs-right}
	 * */
	public function setAlign ($_align) {
		$this->align = $_align;
	}


	/**
	 * Definiert die Anzeigeposition
	 * @param int $_id	: Id des Elements, welches als aktiv angezeigt werden soll
	 * */
	public function setActive ($_id) {
		$this->active = $_id;
	}


	/**
	 * Prueft, ob ein aktives Element bereits gesetzt wurde
	 * */
	public function activeExists () {
		if ($this->active === NULL) {
			return false;
		}

		return true;
	}


	/**
	 * Erstellt einen neuen Eintrag
	 * @return Bootstrap_Nav_Entry
	 * */
	public function addEntry ($_id, $_title, $_content) {
		return $this->arrList[$_id] = new Bootstrap_Nav_Entry($_id, $_title, $_content);
	}


	private function getNavList (Bootstrap_Nav_List $oNavList) {
		$contentMenu	= '';
		$content		= '';
		$list = $oNavList->getList();

		if (!$oNavList->getCount()) {
			return NULL;
		}

		/* @var $oEntry Bootstrap_Nav_Entry */
		foreach ($list as $key => $oEntry) {
			$contentMenu .= '<li><a href="#'.$oEntry->getId().'" data-toggle="tab">'.$oEntry->getTitle().'</a></li>';
			$content .= '<div class="tab-pane" id="'.$oEntry->getId().'">
							'.$oEntry->getContent().'
						</div>';
			}

			$contentMenu = '<ul class="dropdown-menu">
								'.$contentMenu.'
							</ul>';
			return array(0=>$contentMenu, 1=>$content);
	}


	public function display () {
		$contentMenu	= '';
		$content		= '';

		if (!count($this->arrList)) {
			return NULL;
		}

		/* @var $oEntry Bootstrap_Nav_Entry */
		foreach ($this->arrList as $key => $oEntry) {
			$active			= ($key == $this->active) ? ' active' : '';
			$dropdown 		= ($oEntry->addList()->getCount()) ? ' dropdown' : '';
			$dropdownToggle	= ($oEntry->addList()->getCount()) ? ' class="dropdown-toggle" data-toggle="dropdown" ' : '';

			$arrNavListContent = $this->getNavList($oEntry->getNavList());
			$contentMenu .= '<li class="'.$active.$dropdown.'">
								<a href="'.$this->baseurl.'#'.$oEntry->getId().'" data-toggle="tab" '.$dropdownToggle.'>'.$oEntry->getTitle().'</a>
								'.$arrNavListContent[0].'
							</li>';

			$content .= $arrNavListContent[1];
			$content .= '<div class="tab-pane'.$active.'" id="'.$oEntry->getId().'">
							'.$oEntry->getContent().'
						</div>';
		}


		$content = '<div class="tab-content">
						'.$content.'
					</div>';

		$contentMenu = '<ul class="nav '.$this->type.'">
							'.$contentMenu.'
						</ul>';

		if ($this->align == 'tabs-below') {
			return '<div class="tabbable '.$this->align.'">
							'.$content.$contentMenu.'
						</div>';
		}

		return '<div class="tabbable '.$this->align.'">
					'.$contentMenu.$content.'
				</div>';
	}


}


?>